package com.snake.mylibrary.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;

import com.snake.mylibrary.utils.LogUtil;

import java.util.ArrayList;
import java.util.Random;

/**
 * 折线图
 */

public class LineChart extends View {

    private int padding;
    private int mWidth;
    private int mHeight;
    private Paint xPaint;
    private Paint yPaint;
    private Point xPoint;
    private Point centerPoint;
    private Point yPoint;
    private Point[] datas = new Point[5];
    private Random random;
    private ArrayList<Point> points;

    public LineChart(Context context) {
        this(context,null);
    }

    public LineChart(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LineChart(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        xPaint = new Paint();
        xPaint.setColor(Color.RED);
        xPaint.setStyle(Paint.Style.FILL);

        yPaint = new Paint();
        yPaint.setColor(Color.BLUE);
        yPaint.setStyle(Paint.Style.FILL);

        padding = 100;
        datas[0] = new Point(0,200);
        datas[1] = new Point(100,100);
        datas[2] = new Point(200,150);
        datas[3] = new Point(300,100);
        datas[4] = new Point(400,50);
        random = new Random();
        points = new ArrayList<>();
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        LogUtil.e("onMeasure");
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if(widthMode==MeasureSpec.EXACTLY){
            mWidth = widthSize;
        }else if(widthMode==MeasureSpec.AT_MOST){
            throw new IllegalArgumentException("给点尺寸吧");
        }
        if(heightMode==MeasureSpec.EXACTLY){
            mHeight = heightSize;
        }else if(widthMode==MeasureSpec.AT_MOST){
            throw new IllegalArgumentException("给点尺寸吧");
        }
        setMeasuredDimension(mWidth,mHeight);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        LogUtil.e("onSizeChanged");
        super.onSizeChanged(w, h, oldw, oldh);
        xPoint = new Point(mWidth-padding,mHeight-padding);
        centerPoint = new Point(padding,mHeight-padding);
        yPoint = new Point(padding,padding);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        LogUtil.e("onDraw");
        super.onDraw(canvas);
        //ｘ轴
        canvas.drawLine(centerPoint.x,centerPoint.y,xPoint.x,xPoint.y,xPaint);
        //y轴
        canvas.drawLine(centerPoint.x,centerPoint.y,yPoint.x,yPoint.y,yPaint);
        //x箭头
        canvas.drawLine(xPoint.x,xPoint.y,xPoint.x-10,xPoint.y-10,xPaint);
        canvas.drawLine(xPoint.x,xPoint.y,xPoint.x-10,xPoint.y+10,xPaint);
        //y箭头
        canvas.drawLine(yPoint.x,yPoint.y,yPoint.x-10,yPoint.y+10,yPaint);
        canvas.drawLine(yPoint.x,yPoint.y,yPoint.x+10,yPoint.y+10,yPaint);
        //添加x轴刻度和文字
        points.clear();
        for (int i=0;i<(mWidth-padding*2)/100;i++){
            canvas.drawLine(centerPoint.x+i*100,centerPoint.y,centerPoint.x+i*100,centerPoint.y-10,xPaint);
            canvas.drawText(String.valueOf(i*100),centerPoint.x+i*100,centerPoint.y+20,xPaint);
            int y = centerPoint.y - random.nextInt(200);
            int x = centerPoint.x + i * 100;
            canvas.drawPoint(x, y,xPaint);

            points.add(new Point(x,y));
        }

        for (int i =0;i<points.size()-1;i++){
            Point point = points.get(i);
            Point pointNext = points.get(i+1);
            canvas.drawLine(point.x,point.y,pointNext.x,pointNext.y,yPaint);

        }
      
    }
}
